﻿package view.popup
{
    import com.core.*;
    import com.sh.game.global.*;
    import flash.events.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.store.*;
    import uiresource.popup.*;
    import vo.*;

    public class ScSureBuyGame extends ScSureBuyUI implements IDialogUI
    {
        private var maxCount:int = 999;
        private var _data:Object;
        private var buyCount:int = 0;

        public function ScSureBuyGame()
        {
            this.resizeHandler();
            GlobalLayer.instance.root.addEventListener(Event.RESIZE, this.resizeHandler);
            btn_close.clickHandler = new Handler(this.close);
            btn_max.clickHandler = new Handler(this.changeCount, [999, false]);
            btn_left.clickHandler = new Handler(this.toPre);
            btn_right.clickHandler = new Handler(this.toNext);
            btn_cancel.clickHandler = new Handler(this.close);
            btn_sure.clickHandler = new Handler(this.sure);
            list_price.renderHandler = new Handler(this.itemRender);
            return;
        }// end function

        public function initEvent() : void
        {
            if (!ti_count.hasEventListener(Event.CHANGE))
            {
                ti_count.addEventListener(Event.CHANGE, this.tiInput);
            }
            return;
        }// end function

        private function itemRender(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Object = null;
            if (param2 < list_price.length)
            {
                (param1.getChildByName("img_type") as Image).url = this.getImgType(this._data.msg.costtype);
                _loc_3 = ConfigDictionary.data.item;
                _loc_4 = ItemObject.create(_loc_3[this._data.msg.costtype]);
                (param1.getChildByName("txt_money") as Label).text = param1.dataSource.txt_money;
            }
            return;
        }// end function

        private function sure() : void
        {
            var _loc_1:* = UserData.getBagData("emptycount");
            if (_loc_1 == 0)
            {
                WarningManager.show(28);
                return;
            }
            var _loc_2:* = this.getMoney(this._data.msg.costtype);
            if (_loc_2 >= int(txt_total.text))
            {
                StoreSender._4buy(this._data.msg.id, this._data.msg.nowprice, this.buyCount);
                this.close();
            }
            else
            {
                WarningManager.show(128, [ConfigDictionary.data.item[this._data.msg.costtype].name, txt_name.text]);
                return;
            }
            return;
        }// end function

        protected function tiInput(event:Event) : void
        {
            this.changeCount(int(ti_count.text));
            return;
        }// end function

        private function toNext() : void
        {
            if (this.buyCount < 999)
            {
                this.changeCount((this.buyCount + 1));
            }
            return;
        }// end function

        private function toPre() : void
        {
            if (this.buyCount > 0)
            {
                this.changeCount((this.buyCount - 1));
            }
            return;
        }// end function

        private function changeCount(param1:int, param2:Boolean = true, param3:Boolean = false) : Boolean
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:Boolean = false;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            if (!param3)
            {
                _loc_4 = UserData.getBagData("emptycount");
                _loc_5 = _loc_4 * this._data.item.overlying;
                _loc_6 = false;
                if (this.maxCount < _loc_5)
                {
                    _loc_5 = this.maxCount;
                    _loc_6 = true;
                }
                if (_loc_5 < param1)
                {
                    param1 = _loc_5;
                    if (param2)
                    {
                        if (_loc_6)
                        {
                            WarningManager.show(54, [param1, txt_name.text]);
                        }
                    }
                }
                _loc_7 = this.getMoney(this._data.msg.costtype);
                _loc_8 = _loc_7 / this._data.msg.nowprice;
                if (_loc_8 < param1)
                {
                    param1 = _loc_8;
                }
                if (param1 == 0)
                {
                    param1 = 1;
                }
            }
            this.buyCount = param1;
            ti_count.text = param1 + "";
            txt_total.text = param1 * this._data.msg.nowprice + "";
            return true;
        }// end function

        public function close() : void
        {
            this.visible = false;
            this.removeEvent();
            return;
        }// end function

        private function removeEvent() : void
        {
            ti_count.removeEventListener(Event.CHANGE, this.tiInput);
            return;
        }// end function

        public function show(param1:Object) : void
        {
            var _loc_2:Array = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_9:Object = null;
            var _loc_10:Array = null;
            var _loc_11:int = 0;
            var _loc_12:int = 0;
            if (!param1)
            {
                return;
            }
            if (param1.showPosition != null)
            {
                this.x = param1.showPosition.x - this.width / 2;
                this.y = param1.showPosition.y - this.height / 2;
            }
            else
            {
                this.x = GlobalLayer.instance.root.stage.stageWidth - this.width >> 1;
                this.y = GlobalLayer.instance.root.stage.stageHeight - this.height >> 1;
            }
            var _loc_5:* = ModelProxy.getValue("data", "storeInfo");
            if (!ModelProxy.getValue("data", "storeInfo"))
            {
                StoreSender._1getStoreInfo();
            }
            this._data = param1;
            this.maxCount = 999;
            var _loc_6:* = ConfigDictionary.data.item;
            if (!this._data.msg)
            {
                _loc_9 = ConfigDictionary.data.storeNew;
                this._data.msg = _loc_9[this._data.storeId];
                if (!this._data.msg)
                {
                    return;
                }
            }
            if (!this._data.item)
            {
                this._data.item = ItemObject.create(_loc_6[this._data.msg.itemid]);
            }
            var _loc_7:* = ItemObject.create(_loc_6[this._data.msg.costtype]);
            img_money.url = this.getImgType(this._data.msg.costtype);
            this._data.item.count = this._data.msg.containnum > 1 ? (this._data.msg.containnum) : ("");
            fi.data = this._data.item;
            txt_name.text = this._data.item.name;
            var _loc_8:Array = [];
            if (this._data.msg.price)
            {
                _loc_8.push({txt_money:"<font color=\'#95938D\'>原价：     " + this._data.msg.price + _loc_7.name + "</font>"});
                _loc_8.push({txt_money:"<font color=\'#BD744A\'>特价：     " + this._data.msg.nowprice + _loc_7.name + "</font>"});
            }
            else
            {
                _loc_8.push({txt_money:"<font color=\'#BD744A\'>售价：     " + this._data.msg.nowprice + _loc_7.name + "</font>"});
            }
            list_price.array = _loc_8;
            redLine.visible = _loc_8.length > 1;
            txt_limit.visible = true;
            if (this._data.msg.limitbuy)
            {
                _loc_10 = this._data.msg.limitbuy.split("#");
                _loc_11 = this.getBuyCount(this._data.msg.id, "buyNum");
                _loc_12 = _loc_10[1];
                if (this._data.msg.alllimitbuy)
                {
                    _loc_2 = this._data.msg.limitbuy.split("#");
                    _loc_3 = this.getBuyCount(this._data.msg.id, "allBuyNum");
                    _loc_4 = _loc_2[1];
                    this.maxCount = _loc_12 - _loc_11 > _loc_4 - _loc_3 ? (_loc_4 - _loc_3) : (_loc_12 - _loc_11);
                }
                else
                {
                    this.maxCount = _loc_12 - _loc_11;
                }
            }
            else if (this._data.msg.alllimitbuy)
            {
                _loc_2 = this._data.msg.limitbuy.split("#");
                _loc_3 = this.getBuyCount(this._data.msg.id, "allBuyNum");
                _loc_4 = _loc_2[1];
                this.maxCount = _loc_4 - _loc_3;
            }
            else
            {
                txt_limit.visible = false;
            }
            txt_limit.text = "限购:" + this.maxCount;
            this.changeCount(1, true, true);
            this.initEvent();
            this.visible = true;
            return;
        }// end function

        public function getBuyCount(param1:int, param2:String) : int
        {
            var _loc_4:Object = null;
            var _loc_3:* = ModelProxy.getValue("data", "storeInfo");
            for each (_loc_4 in _loc_3)
            {
                
                if (_loc_4.storeId == param1)
                {
                    return _loc_4[param2];
                }
            }
            return 0;
        }// end function

        private function resizeHandler(event:Event = null) : void
        {
            this.x = GlobalLayer.instance.root.stage.stageWidth - this.width >> 1;
            this.y = GlobalLayer.instance.root.stage.stageHeight - this.height >> 1;
            return;
        }// end function

        private function getImgType(param1:int) : String
        {
            var _loc_2:String = null;
            switch(param1)
            {
                case 3:
                {
                    _loc_2 = "png.common.cs2_yuanbao";
                    break;
                }
                case 6:
                {
                    _loc_2 = "png.common.cs2_jinbi";
                    break;
                }
                case 1:
                {
                    _loc_2 = "png.common.bgold";
                    break;
                }
                case 2:
                {
                    _loc_2 = "png.common.cs2_qian";
                    break;
                }
                default:
                {
                    _loc_2 = "png.common.jifen";
                    break;
                    break;
                }
            }
            return _loc_2;
        }// end function

        private function getMoney(param1:int) : Number
        {
            var _loc_2:* = UserData.getBagData();
            var _loc_3:Number = 0;
            switch(param1)
            {
                case 1:
                {
                    _loc_3 = _loc_2.bgold;
                    break;
                }
                case 3:
                {
                    _loc_3 = _loc_2.money;
                    break;
                }
                case 2:
                {
                    _loc_3 = _loc_2.bonus;
                    break;
                }
                case 5:
                {
                    _loc_3 = _loc_2.contribution;
                    break;
                }
                case 6:
                {
                    _loc_3 = _loc_2.gold;
                    break;
                }
                case 9:
                {
                    _loc_3 = _loc_2.zuoqi;
                    break;
                }
                case 10:
                {
                    _loc_3 = _loc_2.yuyi;
                    break;
                }
                case 11:
                {
                    _loc_3 = _loc_2.shengwang;
                    break;
                }
                case 15:
                {
                    _loc_3 = _loc_2.jifen;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return _loc_3;
        }// end function

    }
}
